package com.xy.biz.fd.controller;

import java.util.Collection;
import java.util.List;


import com.xy.biz.fd.client.DocumentRubbishClient;
import com.xy.biz.fd.domain.converter.DocumentRubbishConverter;
import com.xy.biz.fd.domain.query.DocumentRubbishQuery;
import com.xy.biz.fd.domain.req.rubbish.*;
import com.xy.biz.fd.domain.resp.GetDocumentRubbishResponse;
import com.xy.biz.fd.service.DocumentRubbishService;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 
 * 
 * @author yzh
 * @date 2021年1月30日
 */
@Slf4j
@RestController
@RequestMapping("/api/fd/documentRubbish")
public class DocumentRubbishController implements DocumentRubbishClient {

	@Autowired
	private DocumentRubbishService documentRubbishService;
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveDocumentRubbish(@RequestBody SaveDocumentRubbishRequest request) {
		documentRubbishService.saveDocumentRubbish(DocumentRubbishConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllDocumentRubbish(@RequestBody Collection<SaveDocumentRubbishRequest> collection) {
		documentRubbishService.saveAllDocumentRubbish(DocumentRubbishConverter.toBO(collection));
		return Result.success();
	}
	
	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateDocumentRubbish(@RequestBody SaveDocumentRubbishRequest request) {
		documentRubbishService.updateDocumentRubbish(DocumentRubbishConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteDocumentRubbish(@RequestBody GetDocumentRubbishRequest request) {
		documentRubbishService.deleteDocumentRubbish(request.getDocumentRubbishId());
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetDocumentRubbishResponse> getDocumentRubbish(@RequestBody GetDocumentRubbishRequest request) {
		return Result.success(DocumentRubbishConverter.toResponse(documentRubbishService.getDocumentRubbish(request.getDocumentRubbishId())));
	}
	
	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetDocumentRubbishResponse>> listDocumentRubbishById(@RequestBody Collection<Long> collection) {
		return Result.success(DocumentRubbishConverter.toResponse(documentRubbishService.listDocumentRubbishById(collection)));
	}
	
	@PostMapping(value = "/list")
	@Override
	public Result<List<GetDocumentRubbishResponse>> listDocumentRubbish(@RequestBody ListDocumentRubbishRequest request) {
		return Result.success(DocumentRubbishConverter.toResponse(documentRubbishService.listDocumentRubbish(DocumentRubbishConverter.toQuery(request))));
	}
	
	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countDocumentRubbish(@RequestBody ListDocumentRubbishRequest request) {
		return Result.success(documentRubbishService.countDocumentRubbish(DocumentRubbishConverter.toQuery(request)));
	}
	
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetDocumentRubbishResponse>> pageDocumentRubbish(@RequestBody ListDocumentRubbishRequest request) {
		DocumentRubbishQuery query = DocumentRubbishConverter.toQuery(request);
		List<GetDocumentRubbishResponse> list = DocumentRubbishConverter.toResponse(documentRubbishService.listDocumentRubbish(query));
		int count = documentRubbishService.countDocumentRubbish(query);
		return Result.success(new Page<>(list, count));
	}
	
}
